package com.new_wear.project.system.group.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.new_wear.common.exception.BusinessException;
import com.new_wear.common.utils.StringUtils;
import com.new_wear.common.utils.security.ShiroUtils;
import com.new_wear.framework.web.domain.Ztree;
import com.new_wear.project.system.access.domain.CstVolunteerGroupAccess;
import com.new_wear.project.system.access.service.ICstVolunteerGroupAccessService;
import com.new_wear.project.system.menu.domain.Menu;
import com.new_wear.project.system.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.new_wear.project.system.group.mapper.CstVolunteerGroupMapper;
import com.new_wear.project.system.group.domain.CstVolunteerGroup;
import com.new_wear.project.system.group.service.ICstVolunteerGroupService;
import com.new_wear.common.utils.text.Convert;

/**
 * 志愿者 - 志愿者团体Service业务层处理
 * 
 * @author new_wear
 * @date 2019-12-31
 */
@Service
public class CstVolunteerGroupServiceImpl implements ICstVolunteerGroupService 
{
    @Autowired
    private CstVolunteerGroupMapper cstVolunteerGroupMapper;
    @Autowired
    private ICstVolunteerGroupAccessService cstVolunteerGroupAccessService;

    /**
     * 查询志愿者 - 志愿者团体
     * 
     * @param id 志愿者 - 志愿者团体ID
     * @return 志愿者 - 志愿者团体
     */
    @Override
    public CstVolunteerGroup selectCstVolunteerGroupById(Integer id)
    {
        return cstVolunteerGroupMapper.selectCstVolunteerGroupById(id);
    }

    /**
     * 查询志愿者 - 志愿者团体列表
     * 
     * @param cstVolunteerGroup 志愿者 - 志愿者团体
     * @return 志愿者 - 志愿者团体
     */
    @Override
    public List<CstVolunteerGroup> selectCstVolunteerGroupList(CstVolunteerGroup cstVolunteerGroup)
    {
        return cstVolunteerGroupMapper.selectCstVolunteerGroupList(cstVolunteerGroup);
    }

    /**
     * 新增志愿者 - 志愿者团体
     * 
     * @param cstVolunteerGroup 志愿者 - 志愿者团体
     * @return 结果
     */
    @Override
    public int insertCstVolunteerGroup(CstVolunteerGroup cstVolunteerGroup)
    {

        return cstVolunteerGroupMapper.insertCstVolunteerGroup(cstVolunteerGroup);
    }

    /**
     * 修改志愿者 - 志愿者团体
     * 
     * @param cstVolunteerGroup 志愿者 - 志愿者团体
     * @return 结果
     */
    @Override
    public int updateCstVolunteerGroup(CstVolunteerGroup cstVolunteerGroup)
    {
        return cstVolunteerGroupMapper.updateCstVolunteerGroup(cstVolunteerGroup);
    }

    /**
     * 删除志愿者 - 志愿者团体对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteCstVolunteerGroupByIds(String ids)
    {
        return cstVolunteerGroupMapper.deleteCstVolunteerGroupByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除志愿者 - 志愿者团体信息
     * 
     * @param id 志愿者 - 志愿者团体ID
     * @return 结果
     */
    @Override
    public int deleteCstVolunteerGroupById(Integer id)
    {
        return cstVolunteerGroupMapper.deleteCstVolunteerGroupById(id);
    }

    @Override
    public List<Ztree> menuTreeData() {
        CstVolunteerGroup cstVolunteerGroup = new CstVolunteerGroup();
        List<CstVolunteerGroup> cstVolunteerGroupList = selectCstVolunteerGroupList(cstVolunteerGroup);
        List<Ztree> ztrees = initZtree(cstVolunteerGroupList);
        return ztrees;
    }

    /**
     * 对象转菜单树
     *
     * @param cstVolunteerGroupList 菜单列表
     * @return 树结构列表
     */
    public List<Ztree> initZtree(List<CstVolunteerGroup> cstVolunteerGroupList)
    {
        return initZtree(cstVolunteerGroupList, null, false);
    }

    /**
     * 对象转菜单树
     *
     cstVolunteerGroupList     * @param roleMenuList 角色已存在菜单列表
     * @param permsFlag 是否需要显示权限标识
     * @return 树结构列表
     */
    public List<Ztree> initZtree(List<CstVolunteerGroup> cstVolunteerGroupList, List<String> roleMenuList, boolean permsFlag)
    {
        List<Ztree> ztrees = new ArrayList<Ztree>();
        boolean isCheck = StringUtils.isNotNull(roleMenuList);
        for (CstVolunteerGroup group : cstVolunteerGroupList)
        {
            Ztree ztree = new Ztree();
            ztree.setId(group.getId().longValue());
            ztree.setpId(group.getParentId().longValue());
            ztree.setName(transMenuName(group, permsFlag));
            ztree.setTitle(group.getTitle());
            if (isCheck)
            {
                ztree.setChecked(roleMenuList.contains(group.getId() + group.getPerms()));
            }
            ztrees.add(ztree);
        }
        return ztrees;
    }

    public String transMenuName(CstVolunteerGroup group, boolean permsFlag)
    {
        StringBuffer sb = new StringBuffer();
        sb.append(group.getTitle());
        if (permsFlag)
        {
            sb.append("<font color=\"#888\">&nbsp;&nbsp;&nbsp;" + group.getPerms() + "</font>");
        }
        return sb.toString();
    }
}
